package com.sheepone.distributor;

import com.lmax.disruptor.EventHandler;
import com.sheepone.distributor.event.EventContext;
import com.sheepone.distributor.event.WorkEvent;
import com.sheepone.distributor.thread.LoadExecutorHolder;
import com.sheepone.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Miss.杨
 * @description
 * @since 2023-12-07 21:54
 */
public class WorkerLineHandler implements EventHandler<WorkEvent> {
    private static final Logger logger = LoggerFactory.getLogger(new Object(){}.getClass().getEnclosingClass());
    private final LoadExecutorHolder threadPool;

    public WorkerLineHandler(LoadExecutorHolder threadPool) {
        this.threadPool = threadPool;
    }

    @Override
    public void onEvent(WorkEvent event, long l, boolean b) throws Exception {
        // todo：这里是浅拷贝应该使用深拷贝
        EventContext context = ObjectUtil.copy(event.getContext());
        threadPool.submit(() -> {
            try {
                event.getListener().onEvent(context);
            }catch (Exception e){
                logger.error("WorkerLineHandler onEvent error , context:{}", context,e);
            }
        });
    }
}
